
package com.example.cia.mysql.dao.impl;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.example.cia.mysql.dao.IQueryForAddress;
import com.example.cia.pojo.user;

public class queryForAddressImpl extends mysqlDaoImpl implements IQueryForAddress {
	
	private user u;

	//获取地址数量
	@Override
	public int getCount() throws ClassNotFoundException, SQLException, IOException {
		List<String> result = query();
		return result.size();
	}

	//获取一列地址
	@Override
	public List<String> getAddress(user u) throws ClassNotFoundException, SQLException, IOException {
		this.u=u;
		return query();
	}
	
	//修改获取表名
	@Override
	public String getTableName(){
		return "device";
	}
	
	//修改查询字段
	@Override
	public String getQueryFieldString(int which){
		return getTableName()+"_detail_address";
	}
	
	//查询一列详细地址
	@Override
	public List<String> query() throws ClassNotFoundException, SQLException, IOException {
		String sql = "SELECT "+getQueryFieldString(0)+" FROM "
	             +getQulifiedTableName()+" WHERE device_address = ?";
		Connection con = new databaseConnectionImpl().getCon();
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setString(1, u.getUser_address());
		ResultSet rs = ps.executeQuery();
		List<String> result = new ArrayList<String>();
		while(rs.next()){
			result.add(rs.getString("device_detail_address"));
		}
		rs.close();
		con.close();
		return result;
	}

}
